package com.oak.broke.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.function.Predicate;

/**
 * @author tanghan
 * @Description swagger配置
 * @date 2023/04/14/ 15:10:00
 */
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket docket() {
        Predicate<RequestHandler> predicate = input -> {
            //只有添加了ApiOperation注解的method才在API中显示
            return input.isAnnotatedWith(ApiOperation.class);
        };
        return new Docket(DocumentationType.SWAGGER_2).groupName("docket").apiInfo(apiInfo()).select()
                .apis(predicate::test)
                .paths(PathSelectors.any()).build();
    }

    //构建api文档的详细信息函数
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("会员服务 API")
                //创建人
                .contact(new Contact("broke-vip", "", ""))
                //版本号
                .version("1.0")
                //描述
                .description("会员服务API")
                .build();
    }
}
